/**********************************************************************************************
* File: s3base_actions.js
* Name: Sal Fuentes Jr.
* Date: 09/06/2008
* Info: javascript file that defines functions that are called from extjs controls
**********************************************************************************************/
var uploadWindow = null;

function loadBuckets() {
  Ext.Msg.alert('Message', 'Piss off, mate!'); 
}

/* Opens a dialog box for users to upload files to S3 */
function openDialog() {
  if(gbCurrentlySelectedBucket) {
    if(!uploadWindow) {
        uploadWindow = new Ext.Window({
            applyTo     : 'upload-win',
            title       : 'File Upload Form',
            layout      : 'form',
            border      : true,
            autoWidth   : true,
            autoHeight  : true,
            height      : 100,
            closeAction : 'hide',
            plain       : true,
            items       : Ext.getCmp('upload-form')
        });
    }
    uploadWindow.show(Ext.getCmp('adds3objbtn'));
  }
  else {
    Ext.Msg.alert('Invalid Bucket', 'Please select a bucket first');
  }  
}

/* Does basic form validatio on credentials form */
function IsCredentialsFormFilledOut() {
  var accessKey = Ext.getCmp('access-key').getValue();
  var secretKey = Ext.getCmp('secret-key').getValue();
  if( accessKey.length < 20 ) {
    Ext.Msg.alert('Error', "Access Key Field must be a 20-char, alphanumeric string");
    return false;
  }
    
  if( secretKey.length < 40 ) {
    Ext.Msg.alert('Error', "Secret Key Field must be a 40-char, alphanumeric string");
    return false;
  }

  return true;
}

/* Prepares an ajax request to remove all selected S3 files/objects */
function removeS3Obj() {
  var gridSelModel = Ext.getCmp('keys-grid').getSelectionModel();
  var selectedRows = gridSelModel.getSelections();
  if( selectedRows.length > 0 ) {
    var keyNames = new Array(selectedRows.length);
    for( i = 0; i < selectedRows.length; i++ ) {
      keyNames[i] = selectedRows[i].get('key');
    }
  
    Ext.Ajax.request({
      url: '/deleteS3Objs',
      success: function() { reloadKeysGrid(gbCurrentlySelectedBucket); },
      params: {
        awsAccessKeyId: Ext.getCmp('access-key').getValue(),
        awsSecretKey: Ext.getCmp('secret-key').getValue(),          
        bucketName: gbCurrentlySelectedBucket.id,
        keyNames: keyNames
      }
    });         
  }
}

function getProperPath(fullPath) {
  var myregexp = /^\/[^\/]*\/(.*)$/; //gets /root/home/is/good and returns home/is/good
  var match = myregexp.exec(fullPath);
  if (match != null && match.length > 1) {
    return match[1];
  }        
}
function reloadKeysGrid(bucketNode) {
  var statusBar = Ext.getCmp('right-statusbar');  //grid's status bar
  statusBar.showBusy();
  var bucketName = bucketNode.id
  if ( s3FoxView == true ) {
    var virtualFolder = getProperPath(bucketNode.getPath());
    Ext.getCmp('keys-grid').getStore().load({
      params: { virtual_folder_path: virtualFolder },
      callback: function() { statusBar.clearStatus({useDefaults:true}); }
    });
  }
  else {
    Ext.getCmp('keys-grid').getStore().load({
      params: { bucket_name: bucketName},
      callback: function() { statusBar.clearStatus({useDefaults:true}); }
    });
  }    
  gbCurrentlySelectedBucket = bucketNode;
} 
